﻿using AutoMapper;
using B.S.Backtrack.Domain.Entities;
using B.S.Backtrack.ErrorCode;
using B.S.Backtrack.Infrastructure.Interface;
using B.S.Backtrack.Write.API.Application.Command.Image;
using MediatR;

namespace B.S.Backtrack.Write.API.Application.CommandHandler.Image
{
    public class DelImageHandler : IRequestHandler<DelImageCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Picture> piRep;
        private readonly IMapper mapper;

        public DelImageHandler(IBaseRepository<Picture> piRep,IMapper mapper)
        {
            this.piRep = piRep;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(DelImageCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var pro = piRep.GetAll().Where(x => x.PictureId == request.PictureId).FirstOrDefault();
            if (pro != null)
            {
                pro.IsDel = true;
                piRep.Update(pro);
            }

            result.Code = ApiEnums.删除成功;
            result.Msg = ApiEnums.删除成功.ToString();
            result.Data = 1;
            return Task.FromResult(result);
        }
    }
}
